<?php
class Admin_VanBanController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
    	$vanbanMapper = Library_Model_VanBanMapper::getInstance();
    	$vanban = $vanbanMapper->vanbanList();
    	$this->view->vanban = $vanban;
    	
    	Zend_Registry::get('logger')->info($vanban);
    	
    	/* if(!$this->_request->isXmlHttpRequest()) {
    		$this->view->paginator = $vanbanMapper->getPaginator();
    	} else {
    		$this->view->currentPage = $vanbanMapper->getPaginator()->getCurrentPageNumber();
    	} */
    	
    	/*if($this->_request->isPost()) {
        	$keywords = $this->_request->getParam('query');
        	$query = Zend_Search_Lucene_Search_QueryParser::parse($keywords);
        	$index = Zend_Search_Lucene::open(APPLICATION_PATH . '/indexes');
        	$hits = $index->find($query);
        	$this->view->results = $hits;
        	$this->view->keywords = $keywords;
        }else{
        	$this->view->results = null;
        }  */
    }

    public function themAction()
    {
    	$this->view->title = 'Thêm văn bản';
    	$formThem = new Admin_Form_VanBanForm();
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/vanban/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$formThem->getElement('tenvanban')->addValidator('Db_NoRecordExists',true, array(
    			'table' => 'tbl_vanban',
    			'field' => 'TenVanBan'
    	));
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$maloaivanban 		= $data['maloaivanban'];
    			$manhom 				= $data['manhom'];
    			$machinhanh			= $data['machinhanh'];
    			$maphongban			= $data['maphongban'];
    			$tenvanban 				= $data['tenvanban'];    			
    			$ngaybanhanh 		= $data['ngaybanhanh'];
    			$ngaycohieuluc		= $data['ngaycohieuluc'];
    			
				$trangthai				= $data['trangthai'];
				$doquantrong			= $data['doquantrong'];
				$nguoiduyet				= $data['nguoiduyet'];
				$noidung					= $data['noidung'];
				
				$info = pathinfo($formThem->tentaptin->getFileName(null,false));
				$filename = $info['filename'];
				$ext = $info['extension']?".".$info['extension']:"";
				$formThem->tentaptin->addFilter(new Zend_Filter_File_Rename(array(
						"target"=>time().$filename.$ext,
						"overwrite"=>true)));
				$tentaptin = $data['tentaptin'];
				$duyet = $data['duyet'];
			
    			$vanbanMapper = Library_Model_VanBanMapper::getInstance();
    			$taptinMapper = Library_Model_TapTinMapper::getInstance();
    			    			
    			$vanbanObj 	= new Library_Model_VanBan();
    			$taptinObj = new Library_Model_TapTin();
    			
    			$vanbanObj->setMaloaivanban($maloaivanban);
    			$vanbanObj->setManhom($manhom);
    			$vanbanObj->setMachinhanh($machinhanh);
    			$vanbanObj->setMaphongban($maphongban);
    			$vanbanObj->setTenvanban($tenvanban);
    			$vanbanObj->setNgay(Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'));
    			$vanbanObj->setNgaybanhanh($ngaybanhanh);
				$vanbanObj->setNgaycohieuluc($ngaycohieuluc);
				$vanbanObj->setTrangthai($trangthai);
				$vanbanObj->setDoquantrong($doquantrong);
				$vanbanObj->setNguoiduyet($nguoiduyet);
				$vanbanObj->setTrichdan($noidung);
				
				$taptinObj->setTentaptin($tentaptin);
				$taptinObj->setDuyet($duyet);

    			$vanban = $vanbanMapper->save($vanbanObj);
    			$taptin = $taptinMapper->save($taptinObj);
    			$this->_redirect('/admin/vanban/index');
    		}
    	}
    }

   public function capnhatAction()
    {
        $this->view->title = 'Cập nhật văn bản';
        $formCapnhat = new Admin_Form_VanBanForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/vanban/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $mavanban = $this->getRequest()->getParam('id');
        
        $formCapnhat->getElement('tenvanban')->addValidator('Db_NoRecordExists',true, array(
        		'table' => 'tbl_vanban',
        		'field' => 'TenVanBan',
        		'exclude'	=> array(
        				'field'=> 'MaVanBan',
        				'value' => $mavanban)
        ));
        
        
        $vanbanMapper = Library_Model_VanBanMapper::getInstance();
        $taptinMapper = Library_Model_TapTinMapper::getInstance();
               
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 							= $data['id'];
        		$maloaivanban 		= $data['maloaivanban'];
        		$manhom 				= $data['manhom'];
        		$machinhanh			= $data['machinhanh'];
        		$maphongban			= $data['maphongban'];
    			$tenvanban 				= $data['tenvanban'];    			
    			$ngaybanhanh 		= $data['ngaybanhanh'];
    			$ngaycohieuluc		= $data['ngaycohieuluc'];
  
				$trangthai				= $data['trangthai'];
				$doquantrong			= $data['doquantrong'];
				$nguoiduyet				= $data['nguoiduyet'];
				$noidung					= $data['noidung'];
				
				$info = pathinfo($formCapnhat->tentaptin->getFileName(null,false));
				$filename = $info['filename'];
				$ext = $info['extension']?".".$info['extension']:"";
				$formCapnhat->tentaptin->addFilter(new Zend_Filter_File_Rename(array(
						"target"=>time().$filename.$ext,
						"overwrite"=>true)));
				$tentaptin = $data['tentaptin'];
				$duyet = $data['duyet'];
			
    			$vanbanObj 	= new Library_Model_VanBan();
    			$taptinObj = new Library_Model_TapTin();
    			    			
    			$vanbanObj->setId($id);
    			$vanbanObj->setMaloaivanban($maloaivanban);
    			$vanbanObj->setManhom($manhom);
    			$vanbanObj->setMachinhanh($machinhanh);
    			$vanbanObj->setMaphongban($maphongban);
    			$vanbanObj->setTenvanban($tenvanban);    			
    			$vanbanObj->setNgay(Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'));
    			$vanbanObj->setNgaybanhanh($ngaybanhanh);//
				$vanbanObj->setNgaycohieuluc($ngaycohieuluc);
			
				$vanbanObj->setTrangthai($trangthai);
				$vanbanObj->setDoquantrong($doquantrong);
				$vanbanObj->setNguoiduyet($nguoiduyet);
				$vanbanObj->setTrichdan($noidung);
				
				$taptinObj->setId($id);
				$taptinObj->setTentaptin($tentaptin);
				$taptinObj->setDuyet($duyet);
    	
    			$vanban = $vanbanMapper->save($vanbanObj);
    			$taptin = $taptinMapper->save($taptinObj);
    			$this->_redirect('/admin/vanban/index');
        	}
        } else {
        		$vanban = $vanbanMapper->searchVanBanById($mavanban);
        		//$taptin = $taptinMapper->searchByMaVanBan($mavanban);
        		
        		$formCapnhat->getElement('id')->setValue($vanban->getId());
        		$formCapnhat->getElement('maloaivanban')->setValue($vanban->getMaloaivanban());        	
        		$formCapnhat->getElement('manhom')->setValue($vanban->getManhom());
        		$formCapnhat->getElement('machinhanh')->setValue($vanban->getMachinhanh());
        		$formCapnhat->getElement('maphongban')->setValue($vanban->getMaphongban());
        		$formCapnhat->getElement('tenvanban')->setValue($vanban->getTenvanban());        		
        		$formCapnhat->getElement('ngaybanhanh')->setValue($vanban->getNgaybanhanh());
				$formCapnhat->getElement('ngaycohieuluc')->setValue($vanban->getNgaycohieuluc());			
				$formCapnhat->getElement('trangthai')->setValue($vanban->getTrangthai());
				$formCapnhat->getElement('doquantrong')->setValue($vanban->getDoquantrong());
				$formCapnhat->getElement('noidung')->setValue($vanban->getTrichdan());
				$formCapnhat->getElement('nguoiduyet')->setValue($vanban->getNguoiduyet());
				/* $formCapnhat->getElement('tentaptin')->setValue($taptin->getTentaptin());
				$formCapnhat->getElement('duyet')->setValue($taptin->getDuyet()); */
			}
        
    } 

    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $mavanban = $this->getRequest()->getParam('id');
        $vanbanMapper = Library_Model_VanBanMapper::getInstance();

        $taptinMapper = Library_Model_TapTinMapper::getInstance();
        $vanbanMapper->delete($mavanban);
        $taptinMapper->delete($mavanban);
        $this->_redirect('/admin/vanban/index');
    }
    
    public function chitietAction(){
    	$vb_id = $this->_request->getParam('id');
    	
    	$vanbanMapper = Library_Model_VanBanMapper::getInstance();
    	$vanban = $vanbanMapper->searchVanBanById($vb_id);
    	$this->view->vanban = $vanban;
    }
/*     public function validateformAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->_helper->viewRenderer()->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Admin_Form_VanBanForm();
        $form->isValid($this->_getAllParams());
        $json = $form->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }
 */

}









